package com.cpfmat.common;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
import java.util.regex.Matcher;

import javax.servlet.http.HttpServletRequest;

import com.cpfmat.entity.OtherUser;

import sun.misc.BASE64Encoder;

public class CommonStringUtils {
    /**
     * 通过request对象获取IP地址
     * @param request
     * @return
     */
    public static String getIpAddress(HttpServletRequest request) {  
        String ip = request.getHeader("x-forwarded-for");  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("WL-Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_CLIENT_IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getRemoteAddr();  
        }  
        return ip;  
    }  
    
    /**
     * 获取UUID
     * @return
     */
    public static String getUUID(){
    	return UUID.randomUUID().toString().replaceAll("-", "");
    }
    
    /**
     * MD5加密
     * @param str
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static String md5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        MessageDigest md5=MessageDigest.getInstance("MD5");
        BASE64Encoder base64en = new BASE64Encoder();
        //加密后的字符串
        String newstr=base64en.encode(md5.digest(str.getBytes("utf-8")));
		return newstr;
    }

	/**
	 * 根据request对象获取basePath
	 * @param request
	 * @return
	 */
	public static String getBasePath(HttpServletRequest request) {
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ (request.getServerPort() == 80 ? "" : ":" + request.getServerPort()) + request.getContextPath();

		return basePath;
	}
	
	/**
	 * 校验手机号格式
	 * @param phoneStr
	 * @return
	 */
	public static boolean validatePhone(String phoneStr){
		Matcher matcher = Contains.phonePattern.matcher(phoneStr);
		return matcher.matches();
	}
	
	/**
	 * 校验邮箱格式
	 * @param emailStr
	 * @return
	 */
	public static boolean validateEmail(String emailStr){
		Matcher matcher = Contains.emailPattern.matcher(emailStr);
		return matcher.matches();
	}
	
	/**
	 * 校验验证码
	 * @param value
	 * @param request
	 * @return
	 */
	public static boolean validate(String value, HttpServletRequest request) {
		String valueCode = (String) request.getSession().getAttribute("strCode");
		
		if (valueCode != null && valueCode.equals(value)) {
			return true;
		}
		return false;
	}
	
	/**
	 * 根据会话获取当前登录用户主键
	 * @param request
	 * @return
	 */
	public static OtherUser getUserFromRequest(HttpServletRequest request){
		OtherUser user = (OtherUser) request.getSession().getAttribute("otherLoginUser");
		return user;
	}
	
	/**
	 * 根据会话获取当前登录用户主键
	 * @param request
	 * @return
	 */
	public static String getUserIdFromRequest(HttpServletRequest request){
		OtherUser user = (OtherUser) request.getSession().getAttribute("otherLoginUser");
		if(user != null)
			return user.getSqlId();
		else
			return null;
	}
}